package todomanager.CONTROLLER;

import java.awt.event.ActionEvent;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.plaf.metal.MetalLookAndFeel;
import javax.swing.plaf.metal.MetalTheme;

import todomanager.MODEL.TodoManagerModel;
import todomanager.View.IconManager;
import todomanager.View.MainFrame;
import todomanager.theme.CustomTheme;

public class MainFrameController {
	private TodoManagerModel todoModel;
	private MainFrame mainFrameView;

	// Sub-Controllers
	private AddFrameController addFrameController;
	private EditFrameController editFrameController;

	private Action showAddFrameAction;
	private Action quitAction;
	private Action deleteTaskAction;
	private Action showEditFrameAction;
	private Action showAboutAction;
	private Action selectUglyThemeAction;
	private Action selectTestThemeAction;	

	public MainFrameController(TodoManagerModel model, MainFrame view) throws UnsupportedEncodingException {
		this.todoModel = model;
		this.mainFrameView = view;

		IconManager icm = new IconManager();

		this.showAddFrameAction = new ShowAddFrameAction(
				view.getLabel("showAddTask"), icm.createIcon("Add16.gif"));
		this.quitAction = new QuitAction(view.getLabel("quitProgram"), null);
		this.deleteTaskAction = new DeleteTaskAction(
				view.getLabel("deleteTask"), icm.createIcon("Delete16.gif"));
		this.showEditFrameAction = new EditTaskAction(
				view.getLabel("editTask"), icm.createIcon("Edit16.gif"));
		
		this.selectUglyThemeAction = new SelectThemeAction(view.getLabel("uglyTheme"), null, "uglytheme");
		this.selectTestThemeAction = new SelectThemeAction(view.getLabel("testTheme"), null, "testtheme");		

		this.addFrameController = new AddFrameController(model, view);
		this.editFrameController = new EditFrameController(model, view);

		this.showAboutAction = new AboutAction(view.getLabel("menuHelpItemAbout"), null);
		// Add Actions to View

		this.mainFrameView.addMenuActions(this.quitAction,
										  this.showAddFrameAction, 
										  this.showEditFrameAction,
										  this.deleteTaskAction, 
										  this.showAboutAction,
										  this.selectUglyThemeAction,
										  this.selectTestThemeAction);
		this.mainFrameView.addButtonActions(this.showAddFrameAction, 
				this.showEditFrameAction, this.deleteTaskAction);

		/*
		 * this.mainFrameView.addButtonActions(this.showAddFrameAction,
		 * this.showEditFrameAction, this.deleteTaskAction);
		 */
		//this.mainFrameView.addDeleteAction(deleteTaskAction);

	}

	public class QuitAction extends AbstractAction {

		public QuitAction(String text, ImageIcon icon) {
			super(text, icon);

			putValue(SHORT_DESCRIPTION,
					mainFrameView.getLabel("quitProgram_Desc"));

		}

		public void actionPerformed(ActionEvent e) {
			try {
				this.saveSettings();
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			System.exit(0);
		}

		private void saveSettings() throws Exception {
			// TODO Auto-generated method stub
			Properties appProperties = new Properties();

			appProperties.setProperty("mainWindowXPos", new Integer(
					mainFrameView.getX()).toString());
			appProperties.setProperty("mainWindowYPos", new Integer(
					mainFrameView.getY()).toString());
			appProperties.setProperty("mainWindowWidth", new Integer(
					mainFrameView.getWidth()).toString());
			appProperties.setProperty("mainWindowHeight", new Integer(
					mainFrameView.getHeight()).toString());

			appProperties.setProperty("addWindowXPos", new Integer(
					mainFrameView.getAddView().getX()).toString());
			appProperties.setProperty("addWindowYPos", new Integer(
					mainFrameView.getAddView().getY()).toString());
			appProperties.setProperty("addWindowWidth", new Integer(
					mainFrameView.getAddView().getWidth()).toString());
			appProperties.setProperty("addWindowHeight", new Integer(
					mainFrameView.getAddView().getHeight()).toString());
			
			
			appProperties.setProperty("editWindowXPos", new Integer(
					mainFrameView.getEditView().getX()).toString());
			appProperties.setProperty("editWindowYPos", new Integer(
					mainFrameView.getEditView().getY()).toString());
			appProperties.setProperty("editWindowWidth", new Integer(
					mainFrameView.getEditView().getWidth()).toString());
			appProperties.setProperty("editWindowHeight", new Integer(
					mainFrameView.getEditView().getHeight()).toString());
			
			
			appProperties.setProperty("currentTheme", 
					mainFrameView.getCurrentTheme());			

			FileOutputStream fos = new FileOutputStream("todo.properties");
			appProperties.store(fos, "TODO manager settingsfile");

			fos.close();

		}

	}

	public class ShowAddFrameAction extends AbstractAction {

		public ShowAddFrameAction(String text, ImageIcon icon) {
			super(text, icon);

			putValue(SHORT_DESCRIPTION,
					mainFrameView.getLabel("showAddTask_Desc"));

		}

		public void actionPerformed(ActionEvent e) {
			mainFrameView.getAddView().setVisible(true);
		}
	}

	public class ShowEditFrameAction extends AbstractAction {

		public ShowEditFrameAction(String text, ImageIcon icon) {
			super(text, icon);

			putValue(SHORT_DESCRIPTION,
					mainFrameView.getLabel("showAddTask_Desc"));

		}

		public void actionPerformed(ActionEvent e) {
			mainFrameView.getEditView().setVisible(true);
		}
	}

	public class DeleteTaskAction extends AbstractAction {
		public DeleteTaskAction(String text, ImageIcon icon) {
			super(text, icon);

			putValue(SHORT_DESCRIPTION,
					mainFrameView.getLabel("deleteTask_Desc"));

		}

		public void actionPerformed(ActionEvent e) {
			try {
				todoModel.deleteTask(mainFrameView.getSelectedTaskKey());
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			//System.out.println(mainFrameView.getSelectedTaskKey());
			mainFrameView.updateTableData();
		}
	}

	public class AboutAction extends AbstractAction {

		public AboutAction(String text, ImageIcon icon) {
			super(text, icon);
			putValue(SHORT_DESCRIPTION, mainFrameView.getLabel("editTask_Desc"));
		}

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			mainFrameView.showAboutFrame();

		}

	}

	//

	public class EditTaskAction extends AbstractAction {
		public EditTaskAction(String text, ImageIcon icon) {
			super(text, icon);
			putValue(SHORT_DESCRIPTION, mainFrameView.getLabel("editTask_Desc"));
		}

		public void actionPerformed(ActionEvent e) {
			mainFrameView.getEditView().setVisible(true);
		}
	}
	
	public class SelectThemeAction extends AbstractAction {
		public SelectThemeAction(String text, ImageIcon icon, String themeName) {
			super(text, icon);
			putValue(SHORT_DESCRIPTION, mainFrameView.getLabel("selectTheme_Desc"));
			this.themeName = themeName;
		}
		
		private String themeName;

		public void actionPerformed(ActionEvent e) {			
			// Set Theme
			MetalTheme theme = new CustomTheme(this.themeName);			
			MetalLookAndFeel.setCurrentTheme(theme);
			try {
				UIManager.setLookAndFeel(new MetalLookAndFeel());
			} catch (Exception ex) {
				System.out.println(ex);
			}
			SwingUtilities.updateComponentTreeUI(mainFrameView);
			mainFrameView.setCurrentTheme(this.themeName);
		}
	}	

}
